//
//  UICollectionView_register.swift
//  MyLibrary
//
//  Created by 周博 on 2020/6/18.
//  Copyright © 2020 ncit. All rights reserved.
//

import UIKit

public extension UICollectionView {
    
    // MARK: CollectionView注册cell
    
    /**
     * 如果有nib, 注册nib, 否则注册cellClass
     */
    func registerCell(cellClass: AnyClass, identifier: String? = nil) {
        
        // 获取不带有target的cell名string
        let cellName = String(describing: cellClass.self)
        
        // 判断是否有对应的xib, 如果有, 注册xib, 否则注册cell
        // mlgb, 之前写 "cellName = cellClass.description(), \(cellName).xib" path一直拿不到...
        let path = Bundle.main.path(forResource: "\(cellName).nib", ofType: nil)

        if path == nil {
            registerCellClass(cellClass: cellClass, identifier: identifier ?? cellClass.description())
        } else {
            registerCell(nibCellClass: cellClass)
        }
    }
    
    func registerCellClass(cellClass: AnyClass, identifier: String? = nil) {
        register(cellClass, forCellWithReuseIdentifier: identifier ?? cellClass.description())
    }

//    func registerCell(nibName: String, identifier: String? = nil) {
//        register(UINib(nibName: nibName, bundle: nil), forCellWithReuseIdentifier: identifier ?? nibName)
//    }

    /**
     * nibCellClass: String(describing: XXX.self) / String(describing: XXX.classForCoder()) 这样不会带有target名
     */
    func registerCell(nibCellClass: AnyClass, identifier: String? = nil) {
        register(UINib(nibName: String(describing: nibCellClass.self), bundle: nil), forCellWithReuseIdentifier: identifier ?? nibCellClass.description())
    }

    // MARK: CollectionView注册header
    func registerHeader(headerClass: AnyClass, identifier: String? = nil) {
        register(headerClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier ?? headerClass.description())
    }

    func registerHeader(nibName: String, identifier: String? = nil) {
        register(UINib(nibName: nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier ?? "\(ZB_ProjectName ?? "").\(nibName)")
    }

    func registerHeader(nibHeaderClass: AnyClass, identifier: String? = nil) {
        register(UINib(nibName: String(describing: nibHeaderClass.self), bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifier ?? nibHeaderClass.description())
    }

    // MARK: CollectionView注册footer

    func registerFooter(footerClass: AnyClass, identifier: String? = nil) {
        register(footerClass, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier ?? footerClass.description())
    }

    func registerFooter(nibName: String, identifier: String? = nil) {
        register(UINib(nibName: nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier ?? "\(ZB_ProjectName ?? "").\(nibName)")
    }

    func registerFooter(nibFooterClass: AnyClass, identifier: String? = nil) {
        register(UINib(nibName: String(describing: nibFooterClass.self), bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: identifier ?? nibFooterClass.description())
    }


    /// 因为collectionview不能像tableview那样, collectionview必须注册cell 才能dequeue, 定义一个数组存储已注册cell的信息
    /// 用这个方法一定要先手动注册cell
    func dequeueCell<T: UICollectionViewCell>(cellClass: T.Type, indexpath: IndexPath) -> T {

        if let cell = self.dequeueReusableCell(withReuseIdentifier: cellClass.description(), for: indexpath) as? T {
            return cell
        } else {
            return cellClass.init()
        }
    }

}
